var currentMenuID;
var hash = $.trim(window.location.href.split('#')[1]);
$().ready(function () {
    $("[data-toggle='tooltip']").tooltip({
        template: '<div class="tooltip" role="tooltip">' +
        '<div class="tooltip-arrow"></div>' +
        '<div class="tooltip-inner" style="width: 80px;padding:10px;">' +
        '</div>' +
        '</div>'
    });

    var menuID = "menu_1";
    if (hash) {
        $.each(menu_detail, function (i, v) {
            if (v.requestUrl == hash) {
                menuID = i;
                var tmp = $("#" + menuID);

                tmp.closest('li').addClass('active');
                tmp.closest('li.xn-openable').addClass('active');

                return false;
            }
        });
    }

    get_content(menuID);

    //divScroll('page-sidebar');

    //绑定菜单点击事件
    $(".gam-menu").on('click', function () {
        $("#" + this.id).closest('li').addClass('active');

        window.location.hash = menu_detail[this.id].requestUrl;

        get_content(this.id);
    });
});

//菜单详情
var menu_detail = {
    menu_1: {
        title: '首页',
        loadContent: true,//是否需要发送一次请求.当模块内容已经载入后,不需要再发送请求了
        requestUrl: '/user/info-account'//第一次请求时的url
    },
    menu_3_1: {
        title: '实名验证',
        loadContent: true,
        requestUrl: '/user/info-real-name'
    },
    menu_3_2: {
        title: '资金操作 - 入金',
        loadContent: true,
        requestUrl: '/money/recharge'
    },
    menu_3_5: {
        title: '资金操作 - 入金',
        loadContent: true,
        requestUrl: '/moneys/recharge'
    },
    menu_3_3: {
        title: '资金操作 - 出金',
        loadContent: true,
        requestUrl: '/money/withdraw'
    },
    menu_3_4:{
        title: '下载专区',
        loadContent: true,
        requestUrl: '/download/index'
    }
};

/**
 * 获取菜单内容
 * @param menu
 */
function get_content(menu) {

    currentMenuID = menu;

    //@todo 小屏隐藏菜单项


    //先隐藏所有页面
    $('.gam-pages').hide();
    //显示当前页面
    var obj = $('#' + menu + '_content');
    obj.show();

    if (!menu_detail[menu]['loadContent']) return false;

    //将当前菜单项强制获取内容改为false
    menu_detail[menu]['loadContent'] = false;

    if (menu_detail[menu]['requestUrl'] == '') {
        obj.html(menu_detail[menu]['title']);
        return false;
    }

    $.get(menu_detail[menu]['requestUrl'], {}, function (content) {
        obj.html(content);

        page_content_onresize();
    });

}

/**
 * 内容滚动条
 */
function divScroll(domID) {
    $("#" + domID).mCustomScrollbar({
        axis: 'y',
        theme: 'minimal',
        autoHideScrollbar: true,
        scrollInertia: 20,
        advanced: {autoScrollOnFocus: false}
    });
}

/**
 * 退出系统
 */
function logout() {
    swal({
        title: '确定退出系统吗？',
        text: " ",
        type: 'warning',
        showCancelButton: true,
        cancelButtonColor: '#d33',
        cancelButtonText: '取消',
        confirmButtonColor: '#3085d6',
        confirmButtonText: '确定'
    }).then(function () {
        $.getJSON('/logout', function (json) {
            window.location.href = '/'
        })
    }).catch(swal.noop)
}

function infoName() {
    swal({
        title: '当前用户未激活，请实名认证？',
        text: " ",
        type: 'warning',
        showCancelButton: true,
        cancelButtonColor: '#d33',
        cancelButtonText: '取消',
        confirmButtonColor: '#3085d6',
        confirmButtonText: '确定'
    }).then(function () {
        $.getJSON('/logout', function (json) {
            window.location.href = '/'
        })
    }).catch(swal.noop)
}


/**
 * 在线客服
 */
function openChat() {
    var url = "http://chat10.live800.com/live800/chatClient/chatbox.jsp?companyID=435520&jid=6737239657&enterurl=http%3A%2F%2Fprofile%2Elive800%2Ecom%2Fprofile%2Fpreview%2Ejsp&pagetitle=%E6%95%88%E6%9E%9C%E9%A2%84%E8%A7%88&pagereferrer=http%3A%2F%2Fprofile%2Elive800%2Ecom%2Fprofile%2FdeleteCompanyUiConfigsNewEmbedScriptAction%2Eaction%3Fids%3D221393%26isFromClient%3D1&firstEnterUrl=http%3A%2F%2Fprofile%2Elive800%2Ecom%2Fprofile%2Fpreview%2Ejsp&lan=zh&tm=1432624449187";
    window.open(url, "_blank", "width=920,height=680,toolbar=no,menubar=no,status=no,location=no");
}

/**
 * 错误提示框
 *
 * @param msg
 * @param close
 */
function setNotify(msg, close) {
    if (close) {
        PNotify.removeAll();
    }

    new PNotify({
        title: '提示',
        text: msg,
        type: 'error',
        styling: 'bootstrap3',
        delay: 5000
    });
}

/**
 * 翻页获取内容时，显示loading信息
 *
 * @param tableID
 * @param columns
 */
function show_loading_table(tableID, columns) {
    var loading = "" +
        "<tr>" +
        "<td colspan='" + columns + "' class='text-center'>" +
        "<img style='margin: 10px;width: 40px;height: 40px;' src='/images/loading.gif'/>" +
        "</td>" +
        "</tr>";

    $('#' + tableID + '>tbody').html(loading);
}


/**
 *
 * @param domCurrent 当前页DOM
 * @param domTotal  总页数DOM
 * @param type 1:首尾页,2:翻页,3:跳页
 * @param direction type=1时该参数表示首页或尾页,type=2时该参数表示上一页或下一页
 * @returns {*}
 */
function getPageNo(domCurrent, domTotal, type, direction) {
    var pageNo;
    var current_pageNo = parseInt($("#" + domCurrent).val());
    if (!current_pageNo) current_pageNo = 1;
    var current_pageTotal = parseInt($.trim($("#" + domTotal).html()));
    if (!current_pageTotal) current_pageTotal = 1;

    if (type == 1) {
        if (direction == 1) {
            pageNo = 1;
        } else {
            pageNo = current_pageTotal;
        }
    } else if (type == 2) {
        if (direction == 1) {
            pageNo = current_pageNo - 1;
            if (pageNo < 1) pageNo = 1;
        } else {
            pageNo = current_pageNo + 1;
            if (pageNo > current_pageTotal) pageNo = current_pageTotal;
        }
    } else {
        pageNo = current_pageNo;
    }

    return pageNo;
}


/**
 * 将时间戳类型转为字符串类型
 * @param unix_timestamp
 * @param type 1显示全部，2显示年月日
 */
function convertDate(unix_timestamp, type) {
    if (unix_timestamp == 0) return '/';

    var str = "";
    var date = new Date();
    date.setTime(unix_timestamp * 1000);
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    if (month < 10) month = "0" + month;
    var day = date.getDate();
    if (day < 10) day = "0" + day;

    str += year + "-" + month + "-" + day;

    if (type == 1) {
        var hour = date.getHours();
        if (hour < 10) hour = "0" + hour;
        var min = date.getMinutes();
        if (min < 10) min = "0" + min;
        var sec = date.getSeconds();
        if (sec < 10) sec = "0" + sec;

        str += " " + hour + ":" + min + ":" + sec;
    }

    return str;
}
